package ccapp.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import ccapp.CardSystem;
import ccapp.util.CreditCardSystemConstants;

import frmsystem.Domain;
import frmsystem.IDomain;
import frmsystem.gui.FrameworkController;
import frmsystem.gui.MainUI;
import frmsystem.gui.OperationPanel;
import frmsystem.gui.PersonalRegistrationGUI;
import frmsystem.gui.ReportUI;
import frmsystem.gui.TransactionButtonPanel;
import frmsystem.gui.TransactionUI;

public class CardController extends FrameworkController {

	private CardRegistrationGUI cardRegistration;
	private CardTransactionGUI cardTrans;
	
	public CardController(){
		
		domain = CardSystem.getInstance();
		
		operationPanel = new CardOperationPanelGUI(580, 50);
		operationPanel.setAddPersonalAccountListener(new PersonalAccountListener());
		operationPanel.setAddInterestListener(new GenerateReportListener());
		
		cardTrans = new CardTransactionGUI(100, 380);
		//transactionButtonPanel.addTransactionListener(new TransactionListener());
		cardTrans.addExitListener(new ExitListener());
		cardTrans.addDepositActionListener(new DepositListener());
		cardTrans.addTransactionListener(new ChargeListener());
		
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		mainUI = new MainUI(operationPanel, cardTrans, table, 700, 700);
		
		mainUI.setVisible(true);
	}
	
	private void updateData(){
		DefaultTableModel model = new DefaultTableModel(domain.getAccountData(), domain.getTitles());
		table.setModel(model);
		table.repaint();
	}

	protected class PersonalAccountListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			cardRegistration = new CardRegistrationGUI();
			cardRegistration.addOkActionListener(new PersonalOkListener());
			cardRegistration.addCancelActionListener(new PersonalCancelListener());
			cardRegistration.setVisible(true);
		}
		
	}
	
	private class ChargeListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			int row = table.getSelectedRow();
			int col = table.getSelectedColumn();
			if(table.isCellSelected(row, col)){
				String val = (String)table.getValueAt(row, 1);
				String partyname = (String)table.getValueAt(row, 0);
				transUI = new TransactionUI(val, partyname);
				transUI.addOkActionListener(new ChargeOkListener());
				transUI.addCancelActionListener(new TransactionCancelListener());
				transUI.setVisible(true);
			}
			
		}
		
	}
	
	protected class DepositListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			int row = table.getSelectedRow();
			int col = table.getSelectedColumn();
			if(table.isCellSelected(row, col)){
				String val = (String)table.getValueAt(row, 1);
				String partyname = (String)table.getValueAt(row, 0);
				transUI = new TransactionUI(val, partyname);
				transUI.addOkActionListener(new DepositOkListener());
				transUI.addCancelActionListener(new TransactionCancelListener());
				transUI.setVisible(true);
			}
			
		}
		
	}
	
	private class DepositOkListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			String amt = transUI.getTxtAmount().getText();
			String accNO = transUI.getTxtAccNo().getText();
			String party = transUI.getParty();
			
			if(!"".equals(amt)){
				double am = Double.parseDouble(amt);
				domain.doTransaction(party, accNO, CreditCardSystemConstants.TRANSACTION_DIPOSIT, am);
				updateData();
			}
			
			transUI.dispose();
		}
		
	}
	
	private class ChargeOkListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			String amt = transUI.getTxtAmount().getText();
			String accNO = transUI.getTxtAccNo().getText();
			String party = transUI.getParty();
			
			if(!"".equals(amt)){
				double am = Double.parseDouble(amt);
				domain.doTransaction(party, accNO, CreditCardSystemConstants.TRANSACTION_CHARGE, am);
				updateData();
			}
			
			transUI.dispose();
			
		}	
		
	}
	
	private class TransactionCancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			transUI.dispose();			
		}
		
	}
	
	private class ReportCloseListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			report.dispose();
		}
		
	}
	
	private class GenerateReportListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			int row = table.getSelectedRow();
			int col = table.getSelectedColumn();
			if(table.isCellSelected(row, col)){
				String val = (String)table.getValueAt(row, 1);
				String text = domain.generateReport(val);
				
				report = new ReportUI(text);
				report.addCloseActionListener(new ReportCloseListener());
				report.setVisible(true);
			}
			
		}
		
	}
	
	
	protected class ExitListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			System.exit(0);			
		}
		
	}
	
	protected class PersonalOkListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			int cardType;
			if(cardRegistration.getChkCheckings().isSelected()){
				cardType = CreditCardSystemConstants.CREDITCARD_GOLD;
			}else if(cardRegistration.getChkSavings().isSelected()){
				cardType = CreditCardSystemConstants.CREDITCARD_SILVER;
			}else{
				cardType = CreditCardSystemConstants.CREDITCARD_COPPER;
			}
			String name = cardRegistration.getTxtName().getText();
			String street = cardRegistration.getTxtStreet().getText();
			String city = cardRegistration.getTxtCity().getText();
			String state = cardRegistration.getTxtState().getText();
			String zip = cardRegistration.getTxtZip().getText();
			String email = cardRegistration.getTxtEmail().getText();
			String cNumber = cardRegistration.getTxtCCNo().getText();
			String exp = cardRegistration.getTxtExpDate().getText();
			cardRegistration.dispose();
			domain.registerPerson(name, street, city, state, zip, email, exp, cardType, cNumber);
			updateData();			
		}
		
	}
	
	protected class PersonalCancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			cardRegistration.dispose();			
		}
		
	}
}
